package com.friend.base.controller.base;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.friend.base.service.base.AssistRecordService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.AssistRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2020-01-07
 */
@Api(tags = {""})
@RestController
@Log4j2
@RequestMapping(value = "/assistRecord", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class AssistRecordController extends BaseController {

    @Autowired
    private AssistRecordService assistRecordService;

    @PostMapping("/saveAssist")
    @ApiOperation(value = "保存道友记点赞", notes = "保存会员发布的道友记点赞", httpMethod = "POST")
    public Result saveNote(@RequestBody AssistRecord record){
        try {
            AssistRecord assistRecord = assistRecordService.getOne(Wrappers.<AssistRecord>lambdaQuery().eq(AssistRecord::getNoteId, record.getNoteId()).eq(AssistRecord::getMemId, record.getMemId()));
            if (ObjectUtil.isNotNull(assistRecord)){
                if (assistRecord.getDelFlag() == 0) {
                    assistRecord.setDelFlag(1);
                }else {
                    assistRecord.setDelFlag(0);
                }
                assistRecord.updateById();
            }else {
                record.insert();
            }
            List<AssistRecord> list = assistRecordService.list(Wrappers.<AssistRecord>lambdaQuery().eq(AssistRecord::getNoteId, record.getNoteId()).eq(AssistRecord::getDelFlag, 0));
            return this.createSuccessResult(String.valueOf(list.size()));
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("保存道友记点赞失败");
        }
    }
}

